//
//  Player.m
//  MathMastersAsteroids
//
//  Created by Jordon Phillips on 3/5/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Player.h"


@implementation Player
@synthesize moving, health, soundToggle;
//sets up the player with the proper initial values
-(void)initPlayer{
	[super init];
	sound =[SoundController alloc];
	[sound initSoundController];
	[sound addSound:@"shot" filetype:@"wav"];
	[sound addSound:@"boom" filetype:@"wav"];
	self.xPos = 120;
	self.yPos = 50;
	self.sprite = [[Sprite alloc] init];
	self.sprite.xPos = self.xPos;
	self.sprite.yPos = self.yPos;
	self.sprite.scale = 25;
	self.sprite.image = 1;
	[self.sprite setDimensions:5];
	bulletTimer = [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(createBullet) userInfo:nil repeats:YES];
	bulletList = [[NSMutableArray alloc] init];
	up=FALSE;
	down=FALSE;
	explode=FALSE;
	self.moving=FALSE;
	self.health=100;
	animation = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(animate) userInfo:nil repeats:YES];

}

//the actions the player performs each iteration of the game loop
-(void)act:(CGPoint)target:(BOOL)touch{
	if(touch&&!explode){
		self.moving = TRUE;
		float touchX = target.y/3.20f;
		float touchY = 100-target.x/3.20f;
		if(touchY>12){//do not move the ship when touching inside the gui
			if(self.xPos+15<touchX-5){
				[self moveX:3];
			}else if(self.xPos+20>touchX+5){
				[self moveX:-3];
			}if(self.yPos<touchY-5){
				[self moveY:3];
				down=TRUE;
				up=FALSE;
			}else if(self.yPos>touchY+5){
				[self moveY:-3];
				down=FALSE;
				up=TRUE;
			}else{
				down=FALSE;
				up=FALSE;
			}
		}
	}else{
		self.moving = FALSE;
		down=FALSE;
		up=FALSE;
	}
	for(Bullet *b in bulletList){
		if(!b.sprite.dead){
			[b act];
		}
	}
	//dont let player go past gui
	if(self.yPos<18){
		self.yPos=18;
		self.sprite.yPos=18;
	}
}

//creates a bullet to be fired
-(void)createBullet{
	if(!self.moving&&!explode){//only creates bullets if the player is not moving
		if(soundToggle)
			[sound playSound:0];
		for(Bullet *b in bulletList){
			if(b.sprite.dead){
				[b reset:self.xPos-5 :self.yPos];//if a bullet is dead reuse that bullet
				return;
			}
		}
		Bullet *newBullet = [[Bullet alloc] init];
		[newBullet initBullet:self.xPos-5 :self.yPos];
		[bulletList addObject:newBullet];//if there is no dead bullets add a new one to the list
		[newBullet release];
	}
}
//returns the size of the bullet list
-(int)getBulletListSize{
	return [bulletList count];
}
//returns the bullet at a specific index of the bulletList
-(Bullet*)getBulletAtIndex:(int)index{
	return [bulletList objectAtIndex:index];
}

//what happens when the palyer collides with an asteroid
-(void)collideWithAsteroid{
	if(self.health<=0){
		explode=TRUE;
		if(soundToggle)
			[sound playSound:1];
		NSLog(@"Player Dead");
	}else{
		self.health-=10;
		NSLog(@"Player Health: %i",self.health);
	}
}
//what happens when the player collides with a number
-(void)collideWithNumber{
	if(self.health<=0){
		explode=TRUE;
		if(soundToggle)
			[sound playSound:1];
		NSLog(@"Player Dead");
	}
}

//changes the frame in the animation
-(void)animate{
	if(explode){
		if(self.sprite.currentFrame<15)
			self.sprite.currentFrame=15;
		self.sprite.currentFrame++;
		if(self.sprite.currentFrame>=24){
			self.sprite.currentFrame=24;
			self.sprite.dead=TRUE;
		}		
	}else if(up){
		if(self.sprite.currentFrame<9)
			self.sprite.currentFrame=8;
		self.sprite.currentFrame++;
		if(self.sprite.currentFrame>=15){
			self.sprite.currentFrame=12;
		}
	}else if(down){
		if(self.sprite.currentFrame<3||self.sprite.currentFrame>=10)
			self.sprite.currentFrame=2;
		self.sprite.currentFrame++;
		if(self.sprite.currentFrame>=9){
			self.sprite.currentFrame=6;
		}
	}else{
		if(self.sprite.currentFrame>=3){
			if(self.sprite.currentFrame==3||self.sprite.currentFrame==9)
				self.sprite.currentFrame=0;
			else
				self.sprite.currentFrame--;
		}else{
			self.sprite.currentFrame++;
			if(self.sprite.currentFrame>=4){
				self.sprite.currentFrame=0;
			}
		}
	}
}
-(void)dealloc{
	[bulletList release];
	[bulletTimer release];
	[sound release];
	[animation release];
	[super dealloc];
}

@end
